//
//  StringExtensions.swift
//  clreader
//
//  Created by colin on 2019/3/13.
//  Copyright © 2019 colin. All rights reserved.
//

import Foundation
import CryptoSwift

extension String {
    
    var md5:String {
        return self.md5()
    }
    
    /// 计算高度
    func size(font:UIFont,containerWidth:CGFloat) -> CGSize {
        let attr = NSAttributedString(string: self, attributes: [.font: font])
        let size = attr.boundingRect(with: CGSize(width: containerWidth, height: CGFloat.greatestFiniteMagnitude), options: NSStringDrawingOptions.usesLineFragmentOrigin, context: nil).size
        return size
    }
    
    var htmlAttributedString:NSMutableAttributedString? {
        do {
            if let data = self.data(using: .unicode, allowLossyConversion: true) {
                let attr = try NSMutableAttributedString(data: data, options:[.documentType: NSAttributedString.DocumentType.html,.characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
                let paraph = NSMutableParagraphStyle()
                paraph.lineSpacing = 8
                attr.addAttributes([.paragraphStyle:paraph], range: NSMakeRange(0, attr.length))
                return attr
            } else {
                return nil
            }
        } catch let err {
            print(err)
            return nil
        }

    }
    
}
